package com.rabbit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.rabbit.service.HuanweiClassicService;
import com.rabbit.service.domain.GroupDataDomain;
import com.rabbit.service.domain.entity.GroupDataEntity;
import com.rabbit.service.model.huanwei.BtKuaibaoData;
import com.rabbit.service.model.huanwei.QpMapOneVo;
import com.rabbit.service.model.huanwei.QpMapTwoVo;
import com.rabbit.service.model.huanwei.RlMapVo;
import com.rabbit.utils.DateUtil;
import com.rabbit.utils.NumberUtil;
import com.rabbit.utils.StringUtil;

@Service
public class HuanweiClassicServiceImpl implements HuanweiClassicService {
	
	@Resource
	private GroupDataDomain groupDataDomain;

	@Override
	public QpMapOneVo qpMapOne(String id, Long start, Long end) {
		List<GroupDataEntity>  list = null;
		if(StringUtil.isNotEmpty(id)) {
			list = groupDataDomain.getListByIdAndTime(id,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}else {
			list = groupDataDomain.getListByTime(DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}
		// 计算每个点 String = 小时，分钟  ||  返回结果为状态
		Map<String, List<String>> map = new HashMap<>();
		for(GroupDataEntity entity : list) {
			String key =entity.getTimeGroup()+","+ entity.getHour() ;
			List<String> statusList = map.get(key) == null ? new ArrayList<String>() : map.get(key);
			statusList.add(entity.getStatus());
			map.put(key, statusList);
		}
		List<List<Integer>>  slist = new ArrayList<>();
		List<List<Integer>>  mlist = new ArrayList<>();
		List<List<Integer>>  ulist = new ArrayList<>();
		
		Set<String> keys = map.keySet();
		for(String key :keys) {
			List<String> cs =  map.get(key);
			String status = cs.get(rand(cs.size()));
			if(status.equals("S")) {
				List<Integer> child = new ArrayList<>();
				child.add(Integer.valueOf(key.split(",")[0]) );
				child.add(Integer.valueOf(key.split(",")[1]) );
				slist.add(child);
			}else if(status.equals("M")) {
				List<Integer> child = new ArrayList<>();
				child.add(Integer.valueOf(key.split(",")[0]) );
				child.add(Integer.valueOf(key.split(",")[1]) );
				mlist.add(child);
			}else if(status.equals("U")) {
				List<Integer> child = new ArrayList<>();
				child.add(Integer.valueOf(key.split(",")[0]) );
				child.add(Integer.valueOf(key.split(",")[1]) );
				ulist.add(child);
			}
		}
		
		QpMapOneVo vo = new QpMapOneVo();
		vo.setUnknow(ulist);
		vo.setMove(mlist);
		vo.setStop(slist);
		System.out.println("====" + JSON.toJSONString(vo) );
		return vo;
	}
	
	private List<List<Integer>> randList(){
		List<List<Integer>>  list = new ArrayList<>();
		for(int i = 0 ;i< 100;i++) {
			List<Integer> child = new ArrayList<>();
			child.add(rand(60));
			child.add(rand(24));
			list.add(child);
		}
		return list ;
	}
	
	private Integer rand(Integer range) {
		Random random = new Random();
		int boundedRandomInt = random.nextInt(range);
		return boundedRandomInt;
	}

	@Override
	public QpMapTwoVo qpMapTwo(String id, Long start, Long end) {
		List<GroupDataEntity>  list = null;
		if(StringUtil.isNotEmpty(id)) {
			list = groupDataDomain.getListByIdAndTime(id,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}else {
			list = groupDataDomain.getListByTime(DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}
		QpMapTwoVo vo = new QpMapTwoVo();
		vo.setData(randListTwo(list));
		return vo;
	}
	
	private List<List<Integer>> randListTwo(List<GroupDataEntity>  list){
		List<List<Integer>>  rt = new ArrayList<>();
		Map<Integer, Integer> smap = new HashMap<>();
		Map<Integer, Integer> mmap = new HashMap<>();
		Map<Integer, Integer> umap = new HashMap<>();
		for(int i = 0 ;i< 3 ;i++) {
			
			for(GroupDataEntity entity :list) {
				if(i==0 && entity.getStatus().equals("S")) {
					int num = smap.get(entity.getHour()) == null ? 0 : smap.get(entity.getHour()); 
					smap.put(entity.getHour(), num+1);
				}else if(i==1 && entity.getStatus().equals("M")) {
					int num = mmap.get(entity.getHour()) == null ? 0 : mmap.get(entity.getHour()); 
					mmap.put(entity.getHour(), num+1);
				}else if(i==2 && entity.getStatus().equals("U")) {
					int num = umap.get(entity.getHour()) == null ? 0 : umap.get(entity.getHour()); 
					umap.put(entity.getHour(), num+1);
				}
			}
		}
		for(int j = 0 ;j<24;j++) {
			// total计算
			int snum = smap.get(j) ==null ? 0 : smap.get(j) ;
			int mnum = mmap.get(j) ==null ? 0 : mmap.get(j) ;
			int unum = umap.get(j) ==null ? 0 : umap.get(j) ;
			BigDecimal total = new BigDecimal(snum+mnum+unum);
			int srange = 0;
			int mrange = 0;
			int urange = 0;
			if(snum != 0) {
				srange = NumberUtil.mul(new BigDecimal(NumberUtil.div(new BigDecimal(snum), total, 2)), new BigDecimal(10)).intValue() ;
			}
			if(snum != 0) {
				mrange = NumberUtil.mul(new BigDecimal(NumberUtil.div(new BigDecimal(mnum), total, 2)), new BigDecimal(10)).intValue() ;
			}
			if(snum != 0) {
				urange = NumberUtil.mul(new BigDecimal(NumberUtil.div(new BigDecimal(unum), total, 2)), new BigDecimal(10)).intValue() ;
			}
			
			List<Integer> s = new ArrayList<>();
			s.add(0);
			s.add(j);
			s.add(srange );
			rt.add(s);
			
			List<Integer> m = new ArrayList<>();
			m.add(1);
			m.add(j);
			m.add(mrange);
			rt.add(m);
			
			List<Integer> u = new ArrayList<>();
			u.add(2);
			u.add(j);
			u.add(urange);
			rt.add(u);
		}
		System.err.println(JSON.toJSONString(rt));
		return rt ;
	}
	
	private List<List<Integer>> randListTwo(){
		List<List<Integer>>  list = new ArrayList<>();
		for(int i = 0 ;i< 3 ;i++) {
			for(int j = 0 ;j< 24;j++) {
				List<Integer> child = new ArrayList<>();
				child.add(i);
				child.add(j);
				child.add(rand(10));
				list.add(child);
			}
		}
		return list ;
	}

	@Override
	public List<BtKuaibaoData> btMapOne(String id, Long start, Long end) {
		List<GroupDataEntity>  list = null;
		if(StringUtil.isNotEmpty(id)) {
			list = groupDataDomain.getListByIdAndTime(id,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}else {
			list = groupDataDomain.getListByTime(DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}
		// 
		int m = 0 ;
		int s = 0 ;
		int u = 0 ;
		for(GroupDataEntity entity : list) {
			if(entity.getStatus().equals("M")) {
				m++;
			}else if(entity.getStatus().equals("S")) {
				s++;
			}else if(entity.getStatus().equals("U")) {
				u++;
			}
		}
		
		List<BtKuaibaoData> rt  =new ArrayList<>();
		BtKuaibaoData d1 = new BtKuaibaoData();
		d1.setName("运行");
		d1.setValue(m);
		rt.add(d1);
		BtKuaibaoData d2 = new BtKuaibaoData();
		d2.setName("停车");
		d2.setValue(s);
		rt.add(d2);
		BtKuaibaoData d3 = new BtKuaibaoData();
		d3.setName("未知");
		d3.setValue(u);
		rt.add(d3);
		return rt;
	}

	@Override
	public List<Integer> zzMapOne(String id, Long start, Long end) {
		List<GroupDataEntity>  list = null;
		if(StringUtil.isNotEmpty(id)) {
			list = groupDataDomain.getListByIdAndTime(id,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}else {
			list = groupDataDomain.getListByTime(DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}
		int times = 0;
		
		int min1 = 0 ;
		int min13 = 0 ;
		int min35 = 0 ;
		int min510 = 0 ;
		int min10m = 0 ;
		for(GroupDataEntity entity : list) {
			if( entity.getStatus().equals("S")) {
					times++;
			}else {
				// 保存至种类中
				if(times >0 && times <=1) {
					min1++;
				}else if(times >0 && times <=3) {
					min13++;
				}else if(times >0 && times <=5) {
					min35++;
				}else if(times >0 && times <=10) {
					min510++;
				}else if(times >0 && times >10) {
					min10m++;
				}
				times =0 ;
			}
		}
		//['1分钟以内', '1-3分钟', '3-5分钟', '5-10分钟', '10分钟以上']
		List<Integer> rt  =new ArrayList<>();
		rt.add(min1);
		rt.add(min13);
		rt.add(min35);
		rt.add(min510);
		rt.add(min10m);
		return rt;
	}

	@Override
	public List<RlMapVo> rlMapOne(String id, Long start, Long end,List<String> list) {
		List<GroupDataEntity>  datas = null;
		if(StringUtil.isNotEmpty(id)) {
			datas = groupDataDomain.getListByIdAndTime(id,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}else {
			datas = groupDataDomain.getListByTime(DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(start))  ,DateUtil.toStringDateTimeNYR(DateUtil.getDateTimeOfTimestamp(end)));
		}
		List<RlMapVo> rt = new ArrayList<>();
		if(list!=null&& list.size()>0) {
			RlMapVo vo = null;
			for(GroupDataEntity entity : datas) {
				if(list.contains(entity.getStatus()) && entity.getMlng()!=null && entity.getMlat()!=null) {
					vo = new RlMapVo();
					vo.setLng(entity.getMlng());
					vo.setLat(entity.getMlat());
					if("S".equals(entity.getStatus()) && list.size()==1) {
						vo.setCount(20);
					}else {
						vo.setCount(1);
					}
					
					rt.add(vo);
				}

			}
		}
		
		
		for(int i = 0;i< 100;i++) {
		
		}
		return rt;
	}

	private float randFloat() {
		Random random = new Random();
		float boundedRandomInt = random.nextFloat()/10;
		return boundedRandomInt;
	}
}
